package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.model.admin.bo.AdminCreateBo;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 21:15
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public MarketStorage storeAvatar(MultipartFile file, UUID code) {
        MarketStorage marketStorage = new MarketStorage();
        String key = code + file.getOriginalFilename().substring(file.getOriginalFilename().length() - 4);
        marketStorage.setId(null);
        marketStorage.setKey(key);
        marketStorage.setSize((int) file.getSize());
        marketStorage.setType(file.getContentType());
        marketStorage.setName(file.getOriginalFilename());
        marketStorage.setUrl("http://localhost:8083/wx/storage/fetch/" + key);
        marketStorage.setAddTime(new Date());
        marketStorage.setUpdateTime(new Date());
        marketStorage.setDeleted(false);

        MarketStorageExample marketStorageExample = new MarketStorageExample();

        marketStorageExample.createCriteria().andUrlEqualTo(marketStorage.getUrl());
        this.marketStorageMapper.insert(marketStorage);
        List<MarketStorage> marketStorages = this.marketStorageMapper.selectByExample(marketStorageExample);
        marketStorage.setId(marketStorages.get(0).getId());
        return marketStorage;
    }

    @Override
    public void deleteStorage(AdminCreateBo adminCreateBo) {
        marketStorageMapper.updateDeleted(adminCreateBo.getAvatar());
    }

    @Override
    public BaseData storageList(BaseParam baseParam, String key, String name) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria().andDeletedEqualTo(false);
        if (key != null) {
            criteria.andKeyLike("%" + key + "%");
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketStorages);
        BaseData baseData = new BaseData();
        baseData.setPage(baseParam.getPage());
        baseData.setPages(pageInfo.getPages());
        baseData.setLimit(baseParam.getLimit());
        baseData.setTotal((int) pageInfo.getTotal());
        baseData.setList(marketStorages);
        return baseData;
    }

    @Override
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        MarketStorageExample example = new MarketStorageExample();
        example.createCriteria().andIdEqualTo(marketStorage.getId());
        marketStorage.setUpdateTime(new Date());
        marketStorageMapper.updateByExampleSelective(marketStorage, example);
        return marketStorage;
    }

    @Override
    public void deleteStorage(MarketStorage marketStorage) {
        marketStorageMapper.updateDeleted(marketStorage.getUrl());
    }
}
